/* Copyright (c) 2006 by M Aamir Maniar 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */

namespace Generic.DataStructures
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class ListWithEvents<T> : IList<T>
    {
        public ListWithEvents()
        {
            _innerList = new List<T>();
        }

        public ListWithEvents(IList<T> list)
        {
            _innerList = list;
        }

        private IList<T> _innerList;
        public IList<T> InnerList
        {
            get { return _innerList; }
            set { _innerList = value; }
        }


        #region IList<T> Members

        public int IndexOf(T item)
        {
            return _innerList.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            ListInsertEventArgs<T> args = new ListInsertEventArgs<T>(index, item);
            OnBeforeInsert(args);
            _innerList.Insert(index, item);
            OnAfterInsert(args);

        }

        public event ListInsertHandler<T> BeforeInsert = null;
        protected virtual void OnBeforeInsert(ListInsertEventArgs<T> e)
        {
            if (BeforeInsert != null)
                BeforeInsert(this, e);
        }

        public event ListInsertHandler<T> AfterInsert = null;
        protected virtual void OnAfterInsert(ListInsertEventArgs<T> e)
        {
            if (AfterInsert != null)
                AfterInsert(this, e);
        }

        public void RemoveAt(int index)
        {
            ListRemoveEventArgs<T> args = new ListRemoveEventArgs<T>(index, this[index]);
            OnBeforeRemove(args);
            _innerList.RemoveAt(index);
            OnAfterRemove(args);
        }

        public event ListRemoveHandler<T> BeforeRemove = null;
        protected virtual void OnBeforeRemove(ListRemoveEventArgs<T> e)
        {
            if (BeforeRemove != null)
                BeforeRemove(this, e);
        }

        public event ListRemoveHandler<T> AfterRemove = null;
        protected virtual void OnAfterRemove(ListRemoveEventArgs<T> e)
        {
            if (AfterRemove != null)
                AfterRemove(this, e);
        }

        public T this[int index]
        {
            get
            {
                return _innerList[index];
            }
            set
            {
                T oldValue = this[index];
                ListSetEventArgs<T> args = new ListSetEventArgs<T>(index, oldValue, value);
                OnBeforeSet(args);
                _innerList[index] = value;
                OnAfterSet(args);
            }
        }

        public event ListSetHandler<T> BeforeSet = null;
        protected virtual void OnBeforeSet(ListSetEventArgs<T> e)
        {
            if (BeforeSet != null)
                BeforeSet(this, e);
        }

        public event ListSetHandler<T> AfterSet = null;
        protected virtual void OnAfterSet(ListSetEventArgs<T> e)
        {
            if (AfterSet != null)
                AfterSet(this, e);
        }

        #endregion

        #region ICollection<T> Members

        public void Add(T item)
        {
            ListAddEventArgs<T> args = new ListAddEventArgs<T>(item);
            OnBeforeAdd(args);
            _innerList.Add(item);
            OnAfterAdd(args);
        }

        public event ListAddHandler<T> BeforeAdd = null;
        protected virtual void OnBeforeAdd(ListAddEventArgs<T> e)
        {
            if (BeforeAdd != null)
                BeforeAdd(this, e);
        }

        public event ListAddHandler<T> AfterAdd = null;
        protected virtual void OnAfterAdd(ListAddEventArgs<T> e)
        {
            if (AfterAdd != null)
                AfterAdd(this, e);
        }

        public void Clear()
        {
            OnBeforeClear(EventArgs.Empty);
            _innerList.Clear();
            OnAfterClear(EventArgs.Empty);
        }

        public event EventHandler BeforeClear = null;
        protected virtual void OnBeforeClear(EventArgs e)
        {
            if (BeforeClear != null)
                BeforeClear(this, e);
        }

        public event EventHandler AfterClear = null;
        protected virtual void OnAfterClear(EventArgs e)
        {
            if (AfterClear != null)
                AfterClear(this, e);
        }



        public bool Contains(T item)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public int Count
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public bool IsReadOnly
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public bool Remove(T item)
        {
            int index = IndexOf(item);
            ListRemoveEventArgs<T> args = new ListRemoveEventArgs<T>(index, item);
            OnBeforeRemove(args);
            bool ret = _innerList.Remove(item);
            OnAfterRemove(args);
            return ret;
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}
